package cn.com.farsgiht.bluetoothdemo.protocol;

import android.R.bool;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class SDFileHelper {
   private Context context;
   private FileOutputStream output=null;
   private Long Filetotal=null;
   
	public SDFileHelper() {
    }
    public SDFileHelper(Context context) {
        super();
        this.context = context;
    }

   
    public Long getFiletotal() {
	return Filetotal;
}
    
    public void setFiletotal(Long filetotal) {
	this.Filetotal = filetotal;
    }



    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, String filecontent) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
            output = new FileOutputStream(filename);
            output.write(filecontent.getBytes());
            //将String字符串以字节流的形式写入到输出流中
            output.close();
            //关闭输出流
        } else Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
    }
    //往SD卡写入文件的方法
    public void savaFileToSD(String filename) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //这里就不要用openFileOutput了,那个是往手机内存中写数据的
           output = new FileOutputStream(filename);
        } else Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show();
    }
    //往SD卡写入数据
    public void savaFileData(byte[] data) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限
    	if (output==null) {
    		Log.e("savaFileData", "错误：output为空");
		}else {
            output.write(data);
            //将String字符串以字节流的形式写入到输出流中
            output.close();
            output=null;
            //关闭输出流
		}

    }
    public void  OutputClose() throws IOException {
    	  output.close();
          output=null;
	}
    //读取SD卡中文件的方法
    //定义读取文件的方法:
    public String readFromSD(String filename) throws IOException {
        StringBuilder sb = new StringBuilder("");
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //打开文件输入流
            FileInputStream input = new FileInputStream(filename);
            byte[] temp = new byte[1024];

            int len = 0;
            //读取文件内容:
            while ((len = input.read(temp)) > 0) {
                sb.append(new String(temp, 0, len));
            }
            //关闭输入流
            input.close();
        }
        return sb.toString();
    }
    //删除文件
    public boolean deleteFile(File file)
    {
        boolean result = false;
        if (file != null)
        {
            try
            {
                File file2 = file;
                file2.delete();
                result = true;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                result = false;
            }
        }
        return result;
    }
    public boolean FileWrite(){
		return false;
    	
    }
}